package com.vf.cloud.paas.common.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class NginxUtil {
	private static String installPath = "E:\\VFCloudPaaS";

	public static void main(String[] args) {
		start(installPath);
	}
	
	public static void start(String softPath) {
		new NginxTask(softPath).run();
	}
	
	public static void stop(String softPath) {
		new NginxTask(softPath).stop();
	}

    static class NginxTask implements Runnable {
    	private String softPath;
    	
    	public NginxTask(String softPath){
    		this.softPath=softPath;
    		genStartBat(installPath);
    		genStopBat(installPath);
    	}
    	
    	public void stop() {
    		executeCmd(String.format("%s/nginx/stop.bat", softPath), "");
    	}
    	
    	
        @Override
        public void run() {
        	stop();
        	executeCmd(String.format("%s/nginx/start.bat", softPath), "");
        }
        
    	boolean executeCmd(String command, String type) {
    		boolean isSucc = false;
    		try {
    			Runtime runtime = Runtime.getRuntime();
    			Process process = runtime.exec("cmd /c start /b " + command);
//    			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
//    			while ((reader.readLine()) != null) {
//    			}
    			process.waitFor();
    		} catch (InterruptedException | IOException e) {
    		}
    		return isSucc;
    	}
    	
    	public static boolean genStartBat(String softPath) {
    		BufferedWriter out = null;
    		try {
    			try {
    				File f = new File(String.format("%s/nginx/start.bat", softPath));
    				if (f.exists()) {
    					f.delete();
    				}
    				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, true)));
    				out.write("@echo off \r\n");
    				out.write(String.format("%s \r\n", softPath.substring(0, 2)));
    				out.write(String.format("cd %s/nginx \r\n", softPath));
    				out.write("start nginx \r\n");
    				out.write("echo \"startNginx1999\" \r\n");
    				out.write("exit");
    				out.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			return true;
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (out != null)
    					out.close();
    			} catch (IOException e) {
    			}
    		}
    		return false;
    	}

    	public static boolean genStopBat(String softPath) {
    		BufferedWriter out = null;
    		try {
    			try {
    				File f = new File(String.format("%s/nginx/stop.bat", softPath));
    				if (f.exists()) {
    					f.delete();
    				}
    				out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, true)));

    				out.write("@echo off \r\n");
    				out.write(String.format("set nginx_path=%s/nginx \r\n", softPath));
    				out.write("echo nginxpath: %nginx_path% \r\n");
    				out.write("cd %nginx_path% \r\n");
    				out.write("tasklist | findstr /i \"nginx.exe\" \r\n");
    				out.write("@echo \"nginx is running, stopping...\"  \r\n");
    				out.write("rem nginx -s stop \r\n");
    				out.write("TASKKILL /F /IM nginx.exe /T \r\n");
    				out.write("echo \"stopNginx1999\" \r\n");
    				out.write("exit");

    				out.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    			return true;
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (out != null)
    					out.close();
    			} catch (IOException e) {
    			}
    		}
    		return false;
    	}
    	
    }

	


}
